package com.wzz.common.animation;

import android.view.animation.Animation;
import android.view.animation.Interpolator;
import android.view.animation.Transformation;

/**
 * 翻转动画，将动画从结尾向开头播放
 * @author ltg
 *
 */


public class ReverseAnimation extends Animation {
  private Animation reverse;
//  private String TAG;
  
  public ReverseAnimation(Animation retrogress) {
//    TAG = getClass().getSimpleName();
    resetReverse(retrogress);
  }

  @Override
  protected ReverseAnimation clone() throws CloneNotSupportedException {
    throw new CloneNotSupportedException();
  }

  
  @Override
  public boolean getTransformation(long currentTime, Transformation outTransformation) {
    super.getTransformation(currentTime, outTransformation);
    boolean flag = true;
    if(reverse.getStartTime() <= 0){
      flag = reverse.getTransformation(currentTime, outTransformation);
    }else{
      flag = reverse.getTransformation(reverse.getStartTime() * 2 + reverse.getStartOffset() + reverse.getDuration() - currentTime, outTransformation);
      if(currentTime >= reverse.getStartTime() + reverse.getStartOffset() + reverse.getDuration()){
        flag = false;
      }
    }
    return flag;
  }
  
  @Override
  public void initialize(int width, int height, int parentWidth, int parentHeight) {
    super.initialize(width, height, parentWidth, parentHeight);
    reverse.initialize(width, height, parentWidth, parentHeight);
  }


  @Override
  public void reset() {
    super.reset();
    reverse.reset();
  }


  @Override
  public void restrictDuration(long durationMillis) {
    super.restrictDuration(durationMillis);
    reverse.restrictDuration(durationMillis);
  }

  @Override
  public void scaleCurrentDuration(float scale) {
    super.scaleCurrentDuration(scale);
    reverse.scaleCurrentDuration(scale);
  }


  @Override
  public void setDetachWallpaper(boolean detachWallpaper) {
    super.setDetachWallpaper(detachWallpaper);
    reverse.setDetachWallpaper(detachWallpaper);
  }

  @Override
  public void setDuration(long durationMillis) {
    super.setDuration(durationMillis);
    reverse.setDuration(durationMillis);
  }

  @Override
  public void setFillAfter(boolean fillAfter) {
    super.setFillAfter(fillAfter);
    reverse.setFillAfter(fillAfter);
  }

  @Override
  public void setFillBefore(boolean fillBefore) {
    super.setFillBefore(fillBefore);
    reverse.setFillBefore(fillBefore);
  }

  @Override
  public void setFillEnabled(boolean fillEnabled) {
    super.setFillEnabled(fillEnabled);
    reverse.setFillEnabled(fillEnabled);
  }

  @Override
  public void setInterpolator(Interpolator i) {
    super.setInterpolator(i);
    reverse.setInterpolator(i);
  }

  @Override
  public void setRepeatCount(int repeatCount) {
    super.setRepeatCount(repeatCount);
    reverse.setRepeatCount(repeatCount);
  }

  @Override
  public void setRepeatMode(int repeatMode) {
    super.setRepeatMode(repeatMode);
    reverse.setRepeatMode(repeatMode);
  }

  @Override
  public void setStartOffset(long startOffset) {
    super.setStartOffset(startOffset);
    reverse.setStartOffset(startOffset);
  }

  @Override
  public void setStartTime(long startTimeMillis) {
    super.setStartTime(startTimeMillis);
    reverse.setStartTime(startTimeMillis);
  }

  @Override
  public void setZAdjustment(int zAdjustment) {
    super.setZAdjustment(zAdjustment);
    reverse.setZAdjustment(zAdjustment);
  }

  @Override
  public void start() {
    super.start();
    reverse.start();
  }

  @Override
  public void startNow() {
    super.startNow();
    reverse.startNow();
  }

  @Override
  public boolean willChangeBounds() {
    return reverse.willChangeBounds();
  }

  @Override
  public boolean willChangeTransformationMatrix() {
    return reverse.willChangeTransformationMatrix();
  }
  
  public void resetReverse(Animation reverseAni){
    reverse = reverseAni;
    if(reverse.isInitialized()){
      setStartTime(reverse.getStartTime());
    }
    setDuration(reverse.getDuration());
    setStartOffset(reverse.getStartOffset());
    setFillEnabled(reverse.isFillEnabled());
    setFillBefore(reverse.getFillBefore());
    setFillAfter(reverse.getFillAfter());
    setInterpolator(reverse.getInterpolator());
    setRepeatCount(reverse.getRepeatCount());
    setRepeatMode(reverse.getRepeatMode());
    setZAdjustment(reverse.getZAdjustment());
    setDetachWallpaper(reverse.getDetachWallpaper());
    ensureInterpolator();
  }
  
}
